#!/usr/bin/python
#####################################
# Copyright (c) openBrain 2019
# Licensed under LGPL v2
#
# This FreeCAD macro will give basic properties of the selected object (volume, boundbox, ...)
#
#
# Version history :
# *0.7 : some typo improvement + commenting
# *0.6 : check if selected object has a valid shape
# *0.5 : beta release
#
#####################################

__Name__ = 'Simple Properties'
__Comment__ = 'Gives basic properties of object (volume, boundbox, ...)'
__Author__ = 'openBrain'
__Version__ = '0.7.1'
__Date__ = '2019-07-10'
__License__ = 'LGPL v2'
__Web__ = 'https://www.freecadweb.org/wiki/Macro_SimpleProperties'
__Wiki__ = 'https://www.freecadweb.org/wiki/Macro_SimpleProperties'
__Icon__ = ''
__Help__ = 'Select an object and run the macro'
__Status__ = 'Beta'
__Requires__ = 'FreeCAD >= 0.17'

__dbg__ = False  # True for debugging.
g_disp_width = 3  # Set the display format of numbers.

from PySide import QtGui

import FreeCAD as app
import FreeCADGui as gui

def cslM(msg): #Print message in console
    app.Console.PrintMessage('\n')
    app.Console.PrintMessage(msg)


def cslW(msg): #Print warning in console
    app.Console.PrintMessage('\n')
    app.Console.PrintWarning(msg)


def cslE(msg): #Print error in console
    app.Console.PrintMessage('\n')
    app.Console.PrintError(msg)


def cslD(msg): #Print debug message in console
    if __dbg__:
        app.Console.PrintMessage('\n')
        app.Console.PrintMessage('Debug: ' + str(msg))

if __dbg__:  ##Clear report view in debug mode
    gui.getMainWindow().findChild(QtGui.QTextEdit, 'Report view').clear()

cslM('Starting Simple Properties macro')

if len(gui.Selection.getSelection()) != 1:
    # If not exactly one object selected, warn user & quit.
    cslE('One and only one object shall be selected ... Exiting')
elif not ('Shape' in gui.Selection.getSelection()[0].PropertiesList):
    # If selected object has no shape, warn user & exit.
    cslE('Selected object has no valid shape ... Exiting')
else:
    obj = gui.Selection.getSelection()[0]
    # Get selected object.
    retStr = ''
    if len(gui.Selection.getSelectionEx()[0].SubObjects) != 1:
        # If several object subobjects have been selected, ignore & warn user.
        cslW('None or several subobject(s) selected, will be ignored')
    else:
        # If one subobject selected.
        objEx = gui.Selection.getSelectionEx()[0].SubObjects[0]
        if isinstance(objEx, Part.Edge):
            # If it's an edge, print its length.
            retStr += 'Edge length: {:.{w}g} mm\n'.format(objEx.Length, w=g_disp_width)
        elif isinstance(objEx, Part.Face):
            # If it's a face, print its area.
            retStr += 'Face area: {:.{w}g} m²\n'.format(objEx.Area / 1000000, w=g_disp_width)
        else:
            # If other (unsupported) type, warn user.
            cslD('Subobject type: ' + str(objEx.ShapeType))
            cslW('Unsupported type of subobject')
    retStr += 'Object volume: {:.{w}g}  l\n'.format(obj.Shape.Volume / 1000000, w=g_disp_width)
    bb = obj.Shape.BoundBox  # Get object's bounding box.
    retStr += 'Object boundbox : {:.{w}g} x {:.{w}g} x {:.{w}g} mm³\n'.format(
        bb.XLength, bb.YLength, bb.ZLength, w=g_disp_width)

    # Display information in a message box.
    QtGui.QMessageBox(QtGui.QMessageBox.Information, 'Object Simple Props', retStr).exec_()
